const { Sequelize } = require("sequelize");
const initModels = require('../models/init-models')
const sequelize = new Sequelize("xdclass-edu", "root", "xdclass.net168", {
  host: "120.79.122.205",
  dialect: "mysql",
  timezone: "+08:00",
});
(async () => {
  try {
    await sequelize.authenticate();
    console.log("Connection has been established successfully.");
  } catch (error) {
    console.error("Unable to connect to the database:", error);
  }
})();

const models = initModels(sequelize)
// 一对多关联查询
models.Category.hasMany(models.Category,{foreignKey:'pid', as:'subCategoryList'})
models.Category.belongsTo(models.Category, { foreignKey: 'pid' })

models.Teacher.hasMany(models.Product, { foreignKey: 'teacher_id' })
models.Product.belongsTo(models.Teacher,{foreignKey:'teacher_id', as:'teacherDetail'})


module.exports = {...models,sequelize} 